﻿using IFix;
using IFix.Core;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using Yunchang;

public enum DllType
{
    CSharpDll=1,
    FirstDll=2,
}
public class MainController : MonoBehaviour {
    public Text test;
    public Transform cube;
    private int curNum = 0;
    private float yValue = 0;
    private float ySpeed = 10;
    public InputField inputField;
    private TCPClient client;
	// Use this for initialization
	void Start () {
        VirtualMachine.Info = (s) => UnityEngine.Debug.Log(s);
        //try to load patch for Assembly-CSharp.dll
        var patch = Resources.Load<TextAsset>("Assembly-CSharp.patch");
        if (patch != null)
        {
            UnityEngine.Debug.Log("loading Assembly-CSharp.patch ...");
            var sw = Stopwatch.StartNew();
            PatchManager.Load(new MemoryStream(patch.bytes));
            UnityEngine.Debug.Log("patch Assembly-CSharp.patch, using " + sw.ElapsedMilliseconds + " ms");
        }
        //try to load patch for Assembly-CSharp-firstpass.dll
        patch = Resources.Load<TextAsset>("Assembly-CSharp-firstpass.patch");
        if (patch != null)
        {
            UnityEngine.Debug.Log("loading Assembly-CSharp-firstpass ...");
            var sw = Stopwatch.StartNew();
            PatchManager.Load(new MemoryStream(patch.bytes));
            UnityEngine.Debug.Log("patch Assembly-CSharp-firstpass, using " + sw.ElapsedMilliseconds + " ms");
        }

        StartCoroutine(MoveCube());
    }

    [Patch]
    // Update is called once per frame
    void Update ()
    {
        //yValue += 1 * Time.deltaTime;
        //cube.localEulerAngles = new Vector3(0, yValue, 0);
    }
    [Patch]
    public void Add()
    {
        curNum += 2;
        test.text = curNum.ToString();
    }
    [Patch]
    public void Sub()
    {
        curNum -= 2;
        test.text = curNum.ToString();
    }
    
    IEnumerator MoveCube()
    {
        while (true)
        {
            yValue += BattleViewCenter.Ins.GetYSpeed() * Time.deltaTime;
            cube.localEulerAngles = new Vector3(0, yValue, 0);
            yield return 1;
        }
    }
    public void ConnectedToServer()
    {
        if (client == null)
        {
            client = new TCPClient();
        }
        else {
            client.CloseTCPClient();
            client = new TCPClient();
        }

        client.StartClient(inputField.text);
    }
}
